const { query } = require('../config/database');

// 前端mock数据
const mockFunds = [
  {
    id: 1,
    code: '000001',
    name: '华夏成长混合',
    type: '混合型',
    category: '成长型',
    manager: '张坤',
    company: '华夏基金',
    establishDate: '2001-12-18',
    totalAssets: 125.6,
    netValue: 2.856,
    dailyReturn: 1.25,
    weeklyReturn: 3.45,
    monthlyReturn: 8.92,
    yearlyReturn: 15.67,
    maxDrawdown: -12.34,
    sharpeRatio: 1.23,
    tags: ['成长型', '混合型', '消费行业', '价值投资'],
    riskLevel: '中高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.2,
    redemptionFee: 0.5,
    benchmark: '沪深300指数',
    description: '专注于成长型股票投资，追求长期资本增值'
  },
  {
    id: 2,
    code: '110022',
    name: '易方达消费行业股票',
    type: '股票型',
    category: '行业主题',
    manager: '萧楠',
    company: '易方达基金',
    establishDate: '2010-08-20',
    totalAssets: 98.3,
    netValue: 3.245,
    dailyReturn: -0.85,
    weeklyReturn: 2.18,
    monthlyReturn: 6.34,
    yearlyReturn: 18.92,
    maxDrawdown: -15.67,
    sharpeRatio: 1.45,
    tags: ['消费行业', '股票型', '主题投资', '白酒'],
    riskLevel: '高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.5,
    redemptionFee: 0.5,
    benchmark: '中证消费指数',
    description: '专注于消费行业投资，重点配置白酒、家电等消费龙头'
  },
  {
    id: 3,
    code: '000002',
    name: '嘉实增长混合',
    type: '混合型',
    category: '增长型',
    manager: '刘彦春',
    company: '嘉实基金',
    establishDate: '2003-07-09',
    totalAssets: 156.8,
    netValue: 4.123,
    dailyReturn: 0.92,
    weeklyReturn: 4.67,
    monthlyReturn: 12.45,
    yearlyReturn: 22.34,
    maxDrawdown: -18.92,
    sharpeRatio: 1.67,
    tags: ['增长型', '混合型', '科技股', '医药'],
    riskLevel: '中高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.2,
    redemptionFee: 0.5,
    benchmark: '中证800指数',
    description: '追求长期增长，重点投资成长性良好的优质企业'
  },
  {
    id: 4,
    code: '161725',
    name: '招商中证白酒指数',
    type: '指数型',
    category: '行业指数',
    manager: '侯昊',
    company: '招商基金',
    establishDate: '2015-05-27',
    totalAssets: 89.2,
    netValue: 1.234,
    dailyReturn: -1.23,
    weeklyReturn: -2.45,
    monthlyReturn: -5.67,
    yearlyReturn: -8.92,
    maxDrawdown: -25.34,
    sharpeRatio: 0.89,
    tags: ['白酒指数', '指数型', '行业主题', '被动投资'],
    riskLevel: '高风险',
    minInvestment: 1000,
    managementFee: 0.6,
    custodianFee: 0.1,
    subscriptionFee: 0.8,
    redemptionFee: 0.5,
    benchmark: '中证白酒指数',
    description: '跟踪中证白酒指数，投资白酒行业龙头企业'
  },
  {
    id: 5,
    code: '000858',
    name: '五粮液',
    type: '股票型',
    category: '个股',
    manager: '张坤',
    company: '易方达基金',
    establishDate: '2001-04-20',
    totalAssets: 45.6,
    netValue: 156.78,
    dailyReturn: 2.34,
    weeklyReturn: 8.92,
    monthlyReturn: 15.67,
    yearlyReturn: 28.45,
    maxDrawdown: -22.34,
    sharpeRatio: 1.89,
    tags: ['个股', '股票型', '白酒', '价值投资'],
    riskLevel: '高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.5,
    redemptionFee: 0.5,
    benchmark: '沪深300指数',
    description: '专注于五粮液股票投资，分享白酒行业成长红利'
  },
  {
    id: 6,
    code: '000963',
    name: '华夏医疗健康混合',
    type: '混合型',
    category: '行业主题',
    manager: '葛兰',
    company: '中欧基金',
    establishDate: '2016-09-29',
    totalAssets: 67.8,
    netValue: 2.456,
    dailyReturn: 1.67,
    weeklyReturn: 5.23,
    monthlyReturn: 14.56,
    yearlyReturn: 32.78,
    maxDrawdown: -16.78,
    sharpeRatio: 1.78,
    tags: ['医疗健康', '混合型', '医药', '创新药'],
    riskLevel: '中高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.2,
    redemptionFee: 0.5,
    benchmark: '中证医药指数',
    description: '专注于医疗健康行业投资，重点配置创新药、医疗器械等'
  },
  {
    id: 7,
    code: '001766',
    name: '汇添富创新医药混合',
    type: '混合型',
    category: '行业主题',
    manager: '刘江',
    company: '汇添富基金',
    establishDate: '2016-09-29',
    totalAssets: 34.5,
    netValue: 1.987,
    dailyReturn: 0.89,
    weeklyReturn: 3.45,
    monthlyReturn: 9.23,
    yearlyReturn: 25.67,
    maxDrawdown: -14.56,
    sharpeRatio: 1.56,
    tags: ['创新医药', '混合型', '医药', '生物科技'],
    riskLevel: '中高风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.2,
    redemptionFee: 0.5,
    benchmark: '中证医药指数',
    description: '投资创新医药企业，分享医药行业创新发展红利'
  },
  {
    id: 8,
    code: '002001',
    name: '华夏回报混合',
    type: '混合型',
    category: '平衡型',
    manager: '蔡向阳',
    company: '华夏基金',
    establishDate: '2003-09-05',
    totalAssets: 78.9,
    netValue: 3.456,
    dailyReturn: 0.45,
    weeklyReturn: 2.34,
    monthlyReturn: 6.78,
    yearlyReturn: 12.45,
    maxDrawdown: -8.92,
    sharpeRatio: 1.34,
    tags: ['平衡型', '混合型', '稳健', '分红'],
    riskLevel: '中风险',
    minInvestment: 1000,
    managementFee: 1.5,
    custodianFee: 0.25,
    subscriptionFee: 1.2,
    redemptionFee: 0.5,
    benchmark: '沪深300指数',
    description: '追求稳健收益，注重风险控制，适合稳健型投资者'
  }
];

async function syncFundData() {
  try {
    console.log('🔄 开始同步基金数据...');

    for (const fund of mockFunds) {
      // 检查基金是否已存在
      const [existingFund] = await query(
        'SELECT id FROM funds WHERE code = ?',
        [fund.code]
      );

      if (existingFund) {
        // 更新现有基金数据
        await query(`
          UPDATE funds SET
            name = ?, company_name = ?, manager_name = ?, fund_type = ?, category = ?,
            risk_level = ?, tags = ?, nav = ?, nav_date = CURDATE(), total_assets = ?,
            establishment_date = ?, daily_return = ?, weekly_return = ?, monthly_return = ?,
            yearly_return = ?, max_drawdown = ?, sharpe_ratio = ?, benchmark = ?,
            management_fee = ?, custodian_fee = ?, subscription_fee = ?, redemption_fee = ?,
            min_investment = ?, description = ?, updated_at = CURRENT_TIMESTAMP
          WHERE code = ?
        `, [
          fund.name, fund.company, fund.manager, fund.type, fund.category,
          fund.riskLevel, JSON.stringify(fund.tags), fund.netValue, fund.totalAssets,
          fund.establishDate, fund.dailyReturn, fund.weeklyReturn, fund.monthlyReturn,
          fund.yearlyReturn, fund.maxDrawdown, fund.sharpeRatio, fund.benchmark,
          fund.managementFee, fund.custodianFee, fund.subscriptionFee, fund.redemptionFee,
          fund.minInvestment, fund.description, fund.code
        ]);
        console.log(`✅ 更新基金: ${fund.code} - ${fund.name}`);
      } else {
        // 插入新基金数据
        await query(`
          INSERT INTO funds (
            code, name, company_name, manager_name, fund_type, category, risk_level,
            tags, nav, nav_date, total_assets, establishment_date, status,
            daily_return, weekly_return, monthly_return, yearly_return,
            max_drawdown, sharpe_ratio, benchmark,
            management_fee, custodian_fee, subscription_fee, redemption_fee,
            min_investment, description
          ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURDATE(), ?, ?, 'active', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        `, [
          fund.code, fund.name, fund.company, fund.manager, fund.type, fund.category,
          fund.riskLevel, JSON.stringify(fund.tags), fund.netValue, fund.totalAssets,
          fund.establishDate, fund.dailyReturn, fund.weeklyReturn, fund.monthlyReturn,
          fund.yearlyReturn, fund.maxDrawdown, fund.sharpeRatio, fund.benchmark,
          fund.managementFee, fund.custodianFee, fund.subscriptionFee, fund.redemptionFee,
          fund.minInvestment, fund.description
        ]);
        console.log(`✅ 新增基金: ${fund.code} - ${fund.name}`);
      }
    }

    console.log('🎉 基金数据同步完成！');

  } catch (error) {
    console.error('❌ 基金数据同步失败:', error);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  syncFundData().then(() => {
    process.exit(0);
  }).catch((error) => {
    console.error('同步失败:', error);
    process.exit(1);
  });
}

module.exports = { syncFundData }; 